package com.wandian.game
{
	import flash.events.*;
		
		public class ContiguousSoundLoop extends EventDispatcher
		{
			private var fSoundsTransitionsTimeInMs_num:Number;
			private var fCurrentSoundOrdinal_int:int;
			private var fIntroSound_rsnd:RandomSound;
			private var fSounds_rsnd_arr:Array;
			private var fPlayingInProgress_bl:Boolean;
			
			public function ContiguousSoundLoop()
			{
				this.fSounds_rsnd_arr = new Array();
				return;
			}// end function
			
			public function addIntroSound(param1:RandomSound) : void
			{
				this.fIntroSound_rsnd = param1;
				return;
			}// end function
			
			private function playNextSound(event:Event = null, param2:Number = -1, param3:Boolean = false) : void
			{
				var _loc_4:RandomSound = null;
				var _loc_5:Number = NaN;
				var _loc_6:SoundChannelEx = null;
				if (param3 && this.fIntroSound_rsnd)
				{
					_loc_4 = this.fIntroSound_rsnd;
				}
				else
				{
					this.selectSoundToPlay();
					_loc_4 = RandomSound(this.fSounds_rsnd_arr[this.fCurrentSoundOrdinal_int]);
				}
				_loc_5 = param2 == -1 ? (this.fSoundsTransitionsTimeInMs_num) : (param2);
				_loc_6 = _loc_4.playSound(_loc_5, this.fSoundsTransitionsTimeInMs_num);
				if (this.fSoundsTransitionsTimeInMs_num > 0)
				{
					_loc_6.addEventListener(SoundChannelEx.EVENT_SEAMLESS_STOP_STARTED, this.onSoundChannelSeamlessStopStarted);
				}
				_loc_6.addEventListener(SoundChannelEx.EVENT_SOUND_CHANNEL_PLAYING_COMPLETED, this.dispatchEvent, false, 3);
				_loc_6.addEventListener(SoundChannelEx.EVENT_SOUND_CHANNEL_PLAYING_COMPLETED, this.onSoundChannelPlayingCompleted, false, 2);
				return;
			}// end function
			
			private function onSoundChannelPlayingCompleted(event:Event) : void
			{
				SoundChannelEx(event.target).removeEventListener(SoundChannelEx.EVENT_SOUND_CHANNEL_PLAYING_COMPLETED, this.onSoundChannelPlayingCompleted);
				if (this.playingInProgress)
				{
					this.playNextSound();
				}
				return;
			}// end function
			
			public function startToPlay(param1:Number = 0, param2:Number = -1) : void
			{
				var _loc_3:Number = NaN;
				if (this.playingInProgress)
				{
					return;
				}
				this.fPlayingInProgress_bl = true;
				this.fSoundsTransitionsTimeInMs_num = param1;
				_loc_3 = param2 == -1 ? (this.fSoundsTransitionsTimeInMs_num) : (param2);
				this.fCurrentSoundOrdinal_int = -1;
				this.playNextSound(null, _loc_3, Boolean(this.fIntroSound_rsnd));
				return;
			}// end function
			
			public function get playingInProgress() : Boolean
			{
				return this.fPlayingInProgress_bl;
			}// end function
			
			public function set soundOn(param1:Boolean) : void
			{
				var _loc_2:int = 0;
				if (this.fIntroSound_rsnd)
				{
					this.fIntroSound_rsnd.soundOn = param1;
				}
				_loc_2 = 0;
				while (_loc_2 < this.fSounds_rsnd_arr.length)
				{
					
					RandomSound(this.fSounds_rsnd_arr[_loc_2]).soundOn = param1;
					_loc_2++;
				}
				return;
			}// end function
			
			private function onSoundChannelSeamlessStopStarted(event:Event) : void
			{
				SoundChannelEx(event.target).removeEventListener(SoundChannelEx.EVENT_SEAMLESS_STOP_STARTED, this.onSoundChannelSeamlessStopStarted);
				SoundChannelEx(event.target).removeEventListener(SoundChannelEx.EVENT_SOUND_CHANNEL_PLAYING_COMPLETED, this.onSoundChannelPlayingCompleted);
				this.dispatchEvent(event);
				if (this.playingInProgress)
				{
					this.playNextSound();
				}
				return;
			}// end function
			
			private function selectSoundToPlay() : void
			{
				if (this.fCurrentSoundOrdinal_int == -1 || this.fCurrentSoundOrdinal_int == (this.fSounds_rsnd_arr.length - 1))
				{
					this.fCurrentSoundOrdinal_int = 0;
				}
				else
				{
					var _loc_1:String = this;
					var _loc_2:* = this.fCurrentSoundOrdinal_int + 1;
					_loc_1.fCurrentSoundOrdinal_int = _loc_2;
				}
				return;
			}// end function
			
			public function stopToPlay(param1:Number = -1) : void
			{
				var _loc_2:Number = NaN;
				_loc_2 = param1 == -1 ? (this.fSoundsTransitionsTimeInMs_num) : (param1);
				this.stopSounds(_loc_2);
				this.fPlayingInProgress_bl = false;
				return;
			}// end function
			
			public function addSound(param1:RandomSound) : void
			{
				this.fSounds_rsnd_arr.push(param1);
				return;
			}// end function
			
			private function stopSound(param1:RandomSound, param2:Number = -1) : void
			{
				var _loc_3:Number = NaN;
				_loc_3 = param2 == -1 ? (this.fSoundsTransitionsTimeInMs_num) : (param2);
				param1.removeEventListener(SoundChannelEx.EVENT_SEAMLESS_STOP_STARTED, this.playNextSound);
				param1.removeEventListener(SoundChannelEx.EVENT_SOUND_CHANNEL_PLAYING_COMPLETED, this.playNextSound);
				param1.stopSound(_loc_3);
				return;
			}// end function
			
			private function stopSounds(param1:Number = -1) : void
			{
				var _loc_2:* = undefined;
				if (this.fIntroSound_rsnd)
				{
					this.stopSound(this.fIntroSound_rsnd, param1);
				}
				_loc_2 = 0;
				while (_loc_2 < this.fSounds_rsnd_arr.length)
				{
					
					this.stopSound(RandomSound(this.fSounds_rsnd_arr[_loc_2]), param1);
					_loc_2 = _loc_2 + 1;
				}
				return;
			}// end function
			
		}
	}
